//
//  GGContractViewController.m
//  ggtool
//
//  Created by engfei on 2017/5/4.
//  Copyright © 2017年 GaiGai. All rights reserved.
//

#import "GGContactViewController.h"
#import <Contacts/Contacts.h>

#import <ContactsUI/ContactsUI.h>

@interface GGContactViewController ()

@property (weak, nonatomic) IBOutlet UITextView *textView;


@end

@implementation GGContactViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton *btn;
    [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    
    
    // 通讯录实体
    CNContact;    // 相当于 ABRecordRef
    CNMutableContact;
    // 通讯录操作对象
    CNContactStore;  // 相当于 ABAddressBookRef
    
    /*
     通讯录实体: CNContact  CNMutableContact
     
     通讯录存储获取实体: CNContactStore
     
     通讯录修改操作实体(增删改查):CNSaveRequest  个人感觉和coredata的使用很像
     */
    CNContactFetchRequest;
    
}



- (IBAction)contact:(UIButton *)sender {
    
    CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
    
    CNContactStore *store = [[CNContactStore alloc] init];
    
    switch (status) {
        case CNAuthorizationStatusNotDetermined: // 未进行授权选择
        {
            // 请求授权
            [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
                if (granted) { // 授权成功
                    [self getContact:store];
                } else {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"授权失败" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    [alert show];
                }
            }];
            
        }
            break;
        case CNAuthorizationStatusRestricted: // 未授权，且用户无法更新，如家长控制情况下
        {
            
        }
            break;
        case CNAuthorizationStatusDenied: // 用户拒绝
        {
            
        }
            break;
        case CNAuthorizationStatusAuthorized: // 已授权
        {
            [self getContact:store];
        }
            break;
            
        default:
            break;
    }

}

- (IBAction)other:(UIButton *)sender {
    
}

- (void)getContact:(CNContactStore *)store {

    
    
    CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:@[CNContactIdentifierKey, CNContactEmailAddressesKey, CNContactBirthdayKey, CNContactImageDataKey, CNContactPhoneNumbersKey, [CNContactFormatter descriptorForRequiredKeysForStyle:CNContactFormatterStyleFullName], [CNContactViewController descriptorForRequiredKeys]]];
    
    NSError *error = nil;
    
    [store enumerateContactsWithFetchRequest:request error:&error usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
        
        // 获取姓名
        NSString *firstName = contact.givenName;
        NSString *lastName = contact.familyName;
        
        TFLog(@"name = %@ %@", firstName, lastName);
        
       // 获取电话号码
        NSArray *phones = contact.phoneNumbers;
        
        // 遍历电话号码
        for (CNLabeledValue *labelValue in phones) {
            CNPhoneNumber *phoneNumber = labelValue.value;
            TFLog(@"phonenumber = %@", phoneNumber.stringValue);
        }
        
    }];
    
    // 通讯录的增删改查 (CNSaveRequest操作后,需要通过通讯录操作对象执行保存的请求)
    CNSaveRequest *saveRequest;
    NSError *executeError;
    // 执行保存请求
    [store executeSaveRequest:saveRequest error:&executeError];
    
}




@end
